<?php

namespace Biz;

class JPusher
{
    private $client;

    function __construct($appKey, $masterSecret)
    {
        $config = C('sdk.jpush');
        $appKey = $appKey ?: $config['app_key'];
        $masterSecret = $masterSecret ?: $config['master_secret'];
        $this->client = new \JPush($appKey, $masterSecret);
    }

    public function push($message, $to, $extras = null)
    {
        try {

            $ret = $this->client->push()
                ->setPlatform(['android', 'ios'])
                // ->addAllAudience()
                ->addAlias($to)
                ->addAndroidNotification($message, '您有新的通知', 1, $extras)
                ->addIosNotification($message, null, null, true, null, $extras)
                // ->setNotificationAlert($message)
                ->setOptions(null, null, null, true)
                // 使用测试环境
                // ->setOptions(null, null, null, false)
                ->send();
            return $ret;
        } catch (\APIRequestException $e) {
            // dump($e);
        }
    }
}
